<?php

class TestController extends Controller {

    public function actionIndex() {
        $path = 'F:\111';
        $myfile = Yii::app()->file->set($path, true);
        $rowset = $myfile->getContents();
        if ($rowset) {
            foreach ($rowset as $row) {
                $nameArray = array();
                $name = '';
                $file = Yii::app()->file->set($row, true);
                $nameArray = explode('_', $file->basename);
                if ($nameArray[1] == 'Shipping Form.xls') {
                    continue;
                }
                array_shift($nameArray);
                $name = implode('_', $nameArray);
                echo $name;
                $file->rename($name);
            }
        }
    }

    public function actionImport($tid) {
        set_time_limit(0);
        header("Content-Type:text/html;charset=utf-8");
        
        $template = IteamAmazonTemplate::model()->findByPk((int)$tid);
        if(!$template){
            die('模板不存在');
        }
        $file = 'F:\111\\'.$template->amazonTemplateName.'.xls';
        
        spl_autoload_unregister(array('YiiBase', 'autoload'));  //注销YII的自动加载，采用手动导入，PHPexcel有自己的加载功能
        include_once(dirname(dirname(__FILE__)) . '\extensions/PHPExcel.php');     //按照自己的路径导入类文件
        include_once(dirname(dirname(__FILE__)) . '\extensions/PHPExcel/IOFactory.php');

        $objPHPExcel = PHPExcel_IOFactory::load($file);           //导入上传的excel文件的数据逻辑
        $objPHPExcel->setActiveSheetIndex(1);
        $ActSheet = $objPHPExcel->getActiveSheet();                                               //获取excel中sheet(0)的数据
        spl_autoload_register(array('YiiBase', 'autoload'));                                                      //重新启用YII的自动载入

        $allRow = $ActSheet->getHighestRow();

        //读取数据
        $dataArr = array();
        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
            $dataRow = array();
            $dataRow['nodeId'] = $ActSheet->getCell('A' . $currentRow)->getValue();
            $dataRow['nodePath'] = $ActSheet->getCell('B' . $currentRow)->getValue(); 
            $nodeArr = explode('/', Helper::wordReplace($dataRow['nodePath']));
            $dataRow['amazonClassName'] = Helper::wordReplace($nodeArr[count($nodeArr)-1],2);
            $dataRow['parent'] = Helper::wordReplace($nodeArr[count($nodeArr)-2],2);
            $dataRow['god'] = Helper::wordReplace($nodeArr[0],2);
            $dataRow['itemType'] = str_replace('item_type_keyword:', '', $ActSheet->getCell('C' . $currentRow)->getValue()); 
            array_pop($nodeArr);
            $dataRow['parentPath'] = Helper::wordReplace(implode('/', $nodeArr),2);            
            $dataArr[] = $dataRow;
            unset($dataRow);
            unset($nodeArr);
        }
        
        $i=0;
        //入库
        foreach($dataArr as $row){
            //nodeId不是唯一，用path判断排重
            $have = IteamAmazonClass::model()->find("amazonClassNodePath='".  Helper::sqlFilter($row['nodePath'])."'");
            if(!$have){
                $model = new IteamAmazonClass;
                if($row['god'] == $row['amazonClassName']){
                    $model->amazonClassParentId=0;
                }else{
                    $parent = IteamAmazonClass::model()->find("amazonClassNodePath='". Helper::sqlFilter($row['parentPath'])."'");
                    if($parent){
                        $model->amazonClassParentId=$parent->amazonClassId;
                    }else{
                        echo $row['amazonClassName'].' 没有父分类<br />';
                        continue;
                    }
                }
                $model->amazonTemplatetId=$tid;
                $model->amazonClassName=$row['amazonClassName'];
                $model->amazonClassNodeId=$row['nodeId'];
                $model->amazonClassNodePath=$row['nodePath'];
                if($model->save()){
                    echo $row['amazonClassName'].' 保存成功<br />';
                    $i++;
                }else{
                    echo $row['amazonClassName'].' 保存失败<br />';
                }
            }
        }
        echo '共导入记录：'.$i;
    }
    
    public function actionUpdateCost(){
        set_time_limit(0);
        $conditon = new CDbCriteria();
        $conditon->select = 'ProductID,ProductModel';
        $conditon->addCondition('AverageCost>0');
        $rowTable = Product::model()->findAll($conditon);
        echo count($rowTable).'<br />';
        foreach ($rowTable as $row){
            Helper::updateAvgCost($row->ProductID);
            echo $row->ProductModel.'<br />';
        }
    }
    
    public function actionFxl(){
        $order = OMS::model()->find("OrderName='GAAA120101'");
        echo $order->OrderName;        
    }

}